package dhc;

import static org.junit.Assert.assertTrue;
import dhc.test.RobotTestBed;
import robocode.control.BattlefieldSpecification;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.RoundEndedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import net.sf.robocode.io.Logger;

/**
 * Illustrates JUnit testing of Robocode robots. This test simply verifies that MistaRoboto always
 * beats SittingDuck.
 * 
 * @author Philip Johnson
 * 
 */
public class TestMistaRobotoBehavior1 extends RobotTestBed {

  /** To keep track of if the robot hie at least 3 walls. */
  public boolean hitwall[] = { false, false, false };

  /** track if robot hit left wall. */
  public boolean leftWallVisited = false;
  /** track if robot hit right wall. */
  public boolean rightWallVisited = false;
  /** track if robot hit top wall. */
  public boolean upperWallVisited = false;
  /** track if robot hit bottom wall. */
  public boolean lowerWallVisited = false;
  /** to count how many times the robot hit the walls. */
  public Integer counter = 0;

  /**
   * Confirms that the robot hits the wall at least 3 times for each round.
   * 
   * @param event is an instance of RoundEndedEvent
   */
  @Override
  public void onRoundEnded(RoundEndedEvent event) {
    for (int i = 0; i < 3; i++) {
      Logger.realOut.print(hitwall[i]);
      String printMessage = "Checking hitwall count" + i + ":";
      assertTrue(printMessage, hitwall[i]);
    }
    for (int i = 0; i < 3; i++) {
      hitwall[i] = false;
    }
    counter = 0;
    leftWallVisited = false;
    rightWallVisited = false;
    upperWallVisited = false;
    lowerWallVisited = false;
  }

  /**
   * At the end of each turn, checks if the robot has reached the wall, and make appropriate changes
   * to the boolean array.
   * 
   * @param event is an instance of TurnEndedEvent.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {

    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    BattlefieldSpecification battleFieldSpec = new BattlefieldSpecification();
    double xPos = robot.getX();
    double yPos = robot.getY();

    double width = battleFieldSpec.getWidth();
    double height = battleFieldSpec.getHeight();

    if (counter != 3) {
      if (xPos < 40 && !leftWallVisited) {
        Logger.realOut.print("entered1\n");
        hitwall[counter] = true;
        leftWallVisited = true;
        counter++;
      }
      else if (xPos > (width - 40) && !rightWallVisited) {
        Logger.realOut.print("entered2\n");
        hitwall[counter] = true;
        rightWallVisited = true;
        counter++;
      }
      else if (yPos < 40 && !lowerWallVisited) {
        Logger.realOut.print("entered3\n");
        hitwall[counter] = true;
        lowerWallVisited = true;
        counter++;
      }
      else if (yPos > (height - 40) && !upperWallVisited) {
        Logger.realOut.print("entered4\n");
        hitwall[counter] = true;
        upperWallVisited = true;
        counter++;
      }
    }

  }

  /**
   * Specifies that SittingDuck and MistaRoboto are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Tracker,dhc.MistaRoboto";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 5;
  }

  /**
   * The actual test, which asserts that MistaRoboto has won every round against SittingDuck.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    // BattleResults[] results = event.getIndexedResults();

  }
}
